/**
 * Created by yongwang on 2015/9/2.
 * 级联框
 */
/*  宝宝树66小区二次改造    add   gct  20170802   start   */
//villageSelect,
    /*  宝宝树66小区二次改造    add   gct  20170802   end   */

(function(){
    function Cascading(cascadData){
        this.currentObj = {};
        this.init(cascadData);
        this.evt();
    }
    var data = {}, c,
        provinceSelect,
        citySelect,
        cityAreaSelect,
        village,
        villageSelect,
        villageDiv;
    Cascading.prototype = {
        init: function(cascadData){
            c = this;
            data = cascadData;
        },
        paint: function(obj,boolean,pre){
            var html = "<label>省:</label> <input type='text' id ='province_"+ pre+"' class='am-form-field c-provinces'>"+
                " <label>市:</label> <input type='text' id ='city_"+ pre+"' class='am-form-field c-citys'>"+
                " <label>区:</label> <input type='text' id ='county_"+ pre+"' class='am-form-field c-cityArea'>"+
                (boolean?" <label>小区:</label> <input id ='village_"+ pre+"' type='text' class='am-form-field c-village'>":"");
            $(obj).html(html);
            currentObj = obj;
            this.province();
        },
        province: function(){
            data.ajax.post(data.provinceUrl, {}, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {
                sessionStorage.province = JSON.stringify(data.data);
                var selectData = {
                    data: data.data,
                    class: "c-provinces",
                    method: c.city,
                    currentObj: currentObj
                };
                provinceSelect = new select(selectData);
            }).error(function (err) {

            });
        },
        city: function(id){
            data.ajax.post(data.cityUrl, {provinceID: id}, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {
                sessionStorage.city = JSON.stringify(data.data);
                var selectData = {
                    data: data.data,
                    class: "c-citys",
                    method: c.cityArea,
                    currentObj: currentObj
                };
                citySelect = new select(selectData);
            }).error(function (err) {

            });
        },
        /*  宝宝树66小区二次改造    mod   gct  20170802   start   */
        cityArea: function(id){
            data.ajax.post(data.cityAreaUrl, {cityID: id}, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {
                sessionStorage.cityArea = JSON.stringify(data.data);
                var selectData = {
                    data: data.data,
                    class: "c-cityArea",
                    method: c.village,
                    currentObj: currentObj
                };
                cityAreaSelect = new select(selectData);
            }).error(function (err) {

            });
        },

        village: function(id){
            data.ajax.post(data.villageUrl, {countyId: id}, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {
                    sessionStorage.village = JSON.stringify(data.data);
                    var selectData = {
                        data: data.data,
                        class: "c-village",
                        method: "",
                        currentObj: currentObj
                    };
                    villageSelect = new select(selectData);
            }).error(function (err) {

            });
        },

        /*village: function(params){
            if(!params.isValid)params.isValid = true;
            data.ajax.post(data.villageUrl, params, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {
                if(villageDiv){
                    villageDiv.parentNode.removeChild(villageDiv);
                };
                villageDiv = document.createElement("div");
                $(currentObj).find(".c-village").after($(villageDiv));
                villageDiv.className = "cascading-div c-village-div";
                $(villageDiv).attr("style","overflow-y: scroll;display: none;width: 150px;border: 1px #74c0f9 solid;background: #FFF;position: absolute;z-index: 1000;color: #323232;max-height: 375px;");
                villageDiv.style.left = $(currentObj).find(".c-village")[0].offsetLeft + "px";


                //$(villageDiv).empty();  //清空上次的记录
                data.data.list.forEach(function(item){
                    var wordNode = item.communityName;   //弹出框里的每一条内容
                    var newDivNode = $("<span>").attr("data-id", item.objId);//设置每个节点的id值
                    newDivNode.attr("style", "display: block;font:14px/25px arial;height:25px;padding:0 8px;cursor: pointer;");
                    newDivNode.html(wordNode).appendTo($(villageDiv));  //追加到弹出框
                });
                //$(villageDiv).hide();
                $(villageDiv).show();
            }).error(function (err) {

            });
        },*/

        getProvince: function(btn){
            return $("#"+btn.dataset.target).find('.c-provinces')[0].dataset.cId;
        },
        getProvinceVal: function(btn){
            return $("#"+btn.dataset.target).find('.c-provinces').val();
        },
        getCity: function(btn){
            return $("#"+btn.dataset.target).find('.c-citys')[0].dataset.cId;
        },
        getCityVal: function(btn){
            return $("#"+btn.dataset.target).find('.c-citys').val();
        },
        getCityArea: function(btn){
            return $("#"+btn.dataset.target).find('.c-cityArea')[0].dataset.cId;
        },
        getCityAreaVal: function(btn){
            return $("#"+btn.dataset.target).find('.c-cityArea').val();
        },

        getVillageId: function(btn){
            return $("#"+btn.dataset.target).find('.c-village')[0].dataset.cId;
        },

        getVillage: function(btn){
            return $("#"+btn.dataset.target).find('.c-village').val();
        },


        evt: function(){
            $(document).on("input",".c-village",function(){
                var params = {};
                if($(currentObj).find(".c-citys")[0].dataset.cId)params.cityId = $(currentObj).find(".c-citys")[0].dataset.cId;else return;
                if($(currentObj).find(".c-cityArea")[0].dataset.cId)params.countyId = $(currentObj).find(".c-cityArea")[0].dataset.cId;
                if($(currentObj).find(".c-village").val().length < 2) return;
                params.communityName = $(this).val();
                c.village(params);
            });
            $(document).on("click",".c-village-div span",function(){
                $(currentObj).find(".c-village").val(this.innerText);
                $(currentObj).find(".c-village")[0].dataset.cId = this.getAttribute('data-id');
                $(currentObj).find(".c-village-div").hide();
            });
        }
    };
    gObj.cascading = Cascading;
}(window));
/*  宝宝树66小区二次改造    mod   gct  20170802   end   */